import request from "@/utils/request";
import { IObject } from "@/api/common/common";
import { GoodsForm } from "./goods.api";
import { SealMealItem, SealMealPageVO } from "../business/material/sealMeal.api";

const GOODS_ORDER_BASE_URL = "/goods/goodsOrder";

const GoodsOrderAPI = {
  /**
   * 获取商品分页列表
   *
   * @param queryParams 查询参数
   */
  getPage(queryParams: GoodsOrderPageQuery) {
    return request<any, PageResult<GoodsOrderPageVO[]>>({
      url: `${GOODS_ORDER_BASE_URL}/loadPage`,
      method: "get",
      params: queryParams,
    });
  },

  /**
   * 获取商品表单详情
   *
   * @param id 商品ID
   * @returns 商品表单详情
   */
  getFormData(id: string) {
    return request<any, GoodsOrderPageVO>({
      url: `${GOODS_ORDER_BASE_URL}/load/${id}`,
      method: "get",
    });
  },

  /**
   * 获取商品表单详情
   *
   * @param id 商品ID
   * @returns 商品表单详情
   */
  detail(id: string) {
    return request<any, GoodsOrderDetail>({
      url: `${GOODS_ORDER_BASE_URL}/detail/${id}`,
      method: "get",
    });
  },

  /**
   * 获取商品材料列表
   * @param id 商品ID
   */
  loadItems(id: string) {
    return request<any, GoodsOrderItem[]>({
      url: `${GOODS_ORDER_BASE_URL}/loadItemByOrderId/${id}`,
      method: "get",
    });
  },

  /**
   * 取消订单
   *
   * @param id 商品ID
   * @returns 商品表单详情
   */
  cancel(id: string) {
    return request<any, boolean>({
      url: `${GOODS_ORDER_BASE_URL}/cancel/${id}`,
      method: "get",
    });
  },

  /**
   * 取消订单
   *
   * @param id 商品ID
   * @returns 商品表单详情
   */
  delivery(deliveryInfo: DeliveryInfo) {
    return request<any, boolean>({
      url: `${GOODS_ORDER_BASE_URL}/delivery`,
      method: "get",
      params: deliveryInfo,
    });
  },
};

export default GoodsOrderAPI;

/**
 * 商品分页查询对象
 */
export interface GoodsOrderPageQuery extends PageQuery {
  status?: GoodsOrderStatus;
}

/** 商品分页对象 */
export interface GoodsOrderPageVO {
  /** ID */
  id?: string;
  /** 订单id */
  orderNo?: string;
  createTime?: string;
  customerId?: string;
  customerName?: string;
  totalPrice?: string;
  deliveryAddress?: string;
  relatedeliveryPhoneName?: string;
  deliveryName?: string;
  paymentType?: number;
  status?: GoodsOrderStatus;
  payTradeNo?: string;
  customerBak?: string;
  deliveryNo?: string;
  updateTime?: string;
}

/** 商品分页对象 */
export interface GoodsOrderDetail {
  /** ID */
  id?: string;
  /** 订单id */
  orderNo?: string;
  createTime?: string;
  customerId?: string;
  customerName?: string;
  totalPrice?: string;
  deliveryAddress?: string;
  relatedeliveryPhoneName?: string;
  deliveryName?: string;
  paymentType?: number;
  status?: GoodsOrderStatus;
  payTradeNo?: string;
  customerBak?: string;
  deliveryNo?: string;
  updateTime?: string;
  bills?: OrderBill[];
  items?: OrderItem[];
}

/** 商品分页对象 */
export interface OrderBill {
  /** ID */
  id?: string;
  /** 订单id */
  orderId?: string;
  cash?: string;
  payNo?: string;
  customerName?: string;
  customerId?: string;
  payAccount?: string;
  inAccount?: string;
  payType?: string;
  createTime?: number;
}

export interface OrderItem {
  /** ID */
  id?: string;
  /** 订单id */
  orderId?: string;
  goodsId?: string;
  goodsName?: string;
  price?: string;
  quantity?: string;
  imageUrl?: string;
}

export interface DeliveryInfo {
  /**物流单号 */
  deliveryNo?: string;
  /**物流公司 */
  deliveryCompany?: string;
  /**订单id */
  orderId?: string;
}

// 订单的商品项
export interface GoodsOrderItem extends GoodsForm {
  type: string;
  totalNum: string;
  saleMeal: GoodsOrderItemMaterial;
}

// 订单商品项的材料
export interface GoodsOrderItemMaterial extends SealMealPageVO {
  items: SealMealItem[];
}

export enum GoodsOrderStatus {
  WAIT_PAY = "WAIT_PAY",
  WAIT_DELIVERY = "WAIT_DELIVERY",
  WAIT_RECEIVE = "WAIT_RECEIVE",
  RECEIVED = "RECEIVED",
  WAIT_COMMENT = "WAIT_COMMENT",
  FINISH = "FINISH",
  CANCEL = "CANCEL",
  REFUND = "REFUND",
}

export const GoodsOrderStatus_LABEL: IObject = {
  [GoodsOrderStatus.WAIT_PAY]: "待付款",
  [GoodsOrderStatus.WAIT_DELIVERY]: "待发货",
  [GoodsOrderStatus.WAIT_RECEIVE]: "待收货",
  [GoodsOrderStatus.RECEIVED]: "已签收",
  [GoodsOrderStatus.WAIT_COMMENT]: "待评价",
  [GoodsOrderStatus.FINISH]: "完成",
  [GoodsOrderStatus.CANCEL]: "取消",
  [GoodsOrderStatus.REFUND]: "退货",
};
